/**
 * Created by Administrator on 2017/9/26.
 */
//获取cookie中的数据
$(function () {
	var mycookie = $.cookie('CartCookie');
	mycookieArr = JSON.parse(mycookie);
	// console.log(mycookie);
	var cart_sum_right = 0;
	ajax({
		url:"../json/list.json",
		success:function (res) {
			// console.log(res);
			if(mycookieArr){//判断cookie中是否有值
				
				for(var i=0;i<mycookieArr.length;i++){
					// console.log(mycookieArr);
					//动态创建购物列表
					$("#cart_list").append($('<tr id="cart_goods_'+mycookieArr[i].goodsId+'" class="cart_goods_'+i+'"><td><div class="radioGroup prdGroup"><label class="radioItem theItem active"><input type="checkbox"></label></div></td><td class="col-img"><a href="goods.html?id='+mycookieArr[i].goodsId+'" class="product-image" title="">' +
						'<img src="../images/listImg/'+mycookieArr[i].goodsId+'.jpg" alt=""></a><div class="col-name"><h2 class="product-name prd-name"><a href="goods.html?id='+mycookieArr[i].goodsId+'">'+res[mycookieArr[i].goodsId-1].brand+res[mycookieArr[i].goodsId-1].title+'</a></h2></div></td><td>'+res[mycookieArr[i].goodsId-1].classify[1].goodsClassify[parseInt(mycookieArr[i].goodsClassify)%(parseInt(mycookieArr[i].goodsId)*100)-1]+'</td><td>¥'+res[mycookieArr[i].goodsId-1].price+'.00</td><td><div class="quantity-form"><input type="button" class="substract numBtn" value="-"><input name="" value="'+mycookieArr[i].goodsSum+'" readonly="true" class="qty" type="text"><input type="button" class="plus numBtn" value="+"></div></td><td><span class="prd-price">'+parseInt(mycookieArr[i].goodsSum)*parseInt(res[mycookieArr[i].goodsId-1].price)+'</span></td><td><a href="javascript:void(0);" class="delete-cart">删除</a></td></tr>'));
					// console.log(parseInt($(".cart_goods_" + i+ " .prd-price").text()));
			
					//总价
					cart_sum_right +=parseInt($(".cart_goods_" + i+ " .prd-price").text());
					
						//为删除按钮绑定点击事件
					(function(arg){
						$("body").on('click',"#cart_goods_"+mycookieArr[arg].goodsId+" .delete-cart",function () {
							console.log($(this).parents("tr").find(".prd-price").text());
							cart_sum_right = cart_sum_right - parseInt($(this).parents("tr").find(".prd-price").text())/2;
							$(".sumCart-num").text("￥"+cart_sum_right+".00");
							$(".totalPrice").text("￥"+cart_sum_right+".00")
							//移除cookie中的值
							$(this).parents("tr").remove();
							mycookieArr.splice(arg,1);
							$.cookie('CartCookie', JSON.stringify(mycookieArr),{ expires: 7 });
						})
					})(i)
					
				}
				// console.log(229 + 447 + 447 + 596);
				$(".sumCart-num").text("￥"+cart_sum_right+".00");
				$(".totalPrice").text("￥"+cart_sum_right+".00")
			}
			
			
			
			
		}//success结尾处
	})//ajax结尾处
	//全选不选
	$("#check_all_none").click(function(){
		$("#cart_list .radioItem input").prop("checked",this.checked);
	})
	// console.log($(".selectPdt-del"));
	//点击删除，删除选中的商品
	$(".selectPdt-del").on("click",function () {
		for(var i=0;i<mycookieArr.length;i++){
			if($("#cart_list .radioItem input").eq(i).prop("checked")){
				$("#cart_list .radioItem input").eq(i).parents("tr").remove();
				mycookieArr.splice(i,1);
				$.cookie('CartCookie', JSON.stringify(mycookieArr),{ expires: 7 });
			}
		}
	})
	
})